package com.jyszkj.map.view

import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.jyszkj.map.constant.MapConstants
import com.jyszkj.map.util.MapManager
import com.jyszkj.osmdroid.map.R
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Marker
import org.osmdroid.views.overlay.infowindow.MarkerInfoWindow

class CustomInfoWindow(
    mapView: MapView,
    terminalType: Int
) : MarkerInfoWindow(
    if (terminalType == MapConstants.MAP_TERMINAL_TYPE_CK)
        R.layout.layout_info_window_ck else R.layout.layout_info_window, mapView
) {

    private val tvTitle: TextView = mView.findViewById(R.id.tv_title)
    private val ivArrow: ImageView = mView.findViewById(R.id.iv_arrow)

    init {
        mView.setOnTouchListener { v, event ->
            false
        }
    }

    override fun onOpen(item: Any) {
        // 设置窗口内容
        val maker = item as Marker
        if (maker.title.isNullOrEmpty()) {
            tvTitle.visibility = View.GONE
            ivArrow.visibility = View.GONE
            return
        }
        tvTitle.text = maker.title
        val obj = maker.relatedObject
        val level = obj?.toString()?.toInt() ?: 0
        when (level) {
            -1 -> {
                ivArrow.setImageResource(R.drawable.ic_down_arrow_current_location_black)
                tvTitle.setTextColor(Color.BLACK)
                tvTitle.setBackgroundResource(R.drawable.bg_info_window_current_location_black)
            }

            0 -> {
                ivArrow.setImageResource(R.drawable.ic_down_arrow_current_location)
                tvTitle.setBackgroundResource(R.drawable.bg_info_window_current_location)
            }

            1 -> {
                ivArrow.setImageResource(R.drawable.ic_down_arrow_blue)
                tvTitle.setBackgroundResource(R.drawable.bg_info_window_blue)
            }

            2 -> {
                ivArrow.setImageResource(R.drawable.ic_down_arrow_yellow)
                tvTitle.setBackgroundResource(R.drawable.bg_info_window_yellow)
            }

            3 -> {
                ivArrow.setImageResource(R.drawable.ic_down_arrow_orange)
                tvTitle.setBackgroundResource(R.drawable.bg_info_window_orange)
            }

            4 -> {
                ivArrow.setImageResource(R.drawable.ic_down_arrow_red)
                tvTitle.setBackgroundResource(R.drawable.bg_info_window_red)
            }
        }

        // 添加动画效果
        // 添加动画效果
        mView.alpha = 0f
        mView.scaleX = 0.8f
        mView.scaleY = 0.8f
        mView.animate()
            .alpha(1f)
            .scaleX(1f)
            .scaleY(1f)
            .setDuration(400)
            .start()
    }
}